package com.example.hadoopdemo.mapreduce;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


/**
 * @author jianwang9@iflytek.com
 * @create 2018-04-02 15:29
 **/
public class MapreduceJob extends Configured implements Tool {
    @Override
    public int run(String[] strings) throws Exception {
        if(strings.length != 2){
            System.out.println("args numbers not compare");
            ToolRunner.printGenericCommandUsage(System.err);
            return -1;
        }
        //job设置
        Job job = Job.getInstance(getConf(),"maprducejob test");

        job.setJarByClass(getClass());

        FileInputFormat.addInputPath(job,new Path(strings[0]));
        FileOutputFormat.setOutputPath(job,new Path(strings[1]));

        job.setMapperClass(MapreduceMapper.class);
        job.setCombinerClass(MapreduceReducer.class);
        job.setReducerClass(MapreduceReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        return job.waitForCompletion(true) ? 0 : 1;
    }


    public static void main(String[] args) throws Exception {
        String[] params = new String[2];
        params[0] = "E:\\testinput\\input.txt";
        params[1] = "E:\\testinput\\output.txt";
        int run = ToolRunner.run(new MapreduceJob(), params);
        System.exit(run);
    }
}
